import { ElMessage } from "element-plus";
import { Http } from "./http";
import type { interTable, interCard } from './type'

export class List extends Http {
    total: number;
    limit: number;
    hasPages: boolean;
    page_index: number;
    list: any[];
    reSetphone: boolean
    labels: Map<string, interTable>;
    labels_card: interCard;
    select: any[];
    url: { getlist: string; delete: string; };
    has_next: boolean;
    search_key: any;
    constructor() {
        super()
        this.hasPages = true
        this.total = 0
        this.limit = 15
        this.page_index = 1
        this.has_next = true
        this.search_key = {}
        this.select = []
        this.labels = new Map()
        this.reSetphone = false
        this.labels_card = {
            title: '',
            img: '',
            tag: '',
            price: '',
            count: '',
            rate: ''
        }
        this.url = {
            getlist: '',
            delete: ''
        }
        this.list = this.utils_proxy_set([], (target, key, value) => {
            if (key !== 'length') {
                return new Proxy(value, this.proxyValue)
            }
        })
    }
    async refresh(page_index?: number, method: 'POST' | 'GET' = 'POST') {
        this.list.splice(0, this.list.length)
        if (page_index)
            this.page_index = page_index
        this.has_next = true
        await this.getList(false, method)
    }
    async getList(reverse?: boolean, method = 'POST'): Promise<any[]> {
        this.loading = true
        if (!this.hasPages) {
            this.limit = 99
        }
        const { limit, page_index, search_key, url } = this
        const options = { method, parames: {}, data: { PageSize: limit, Page: page_index, ...search_key } }
        if (method === 'GET') {
            options.parames = options.data
            delete options.data
        }
        await this.befaultGetList(options)
        const res = await this.request(url.getlist, options)
        if (res) {
            const { data: list, dataCount: total } = res
            this.total = total
            if (!list) throw new Error('list数据不存在')
            this.has_next = page_index * limit < total
            if (this.has_next) {
                this.page_index += 1
            }
            if (reverse) {
                this.list.unshift(...list)
            } else {
                this.list.push(...list)
            }
            this.loading = false
            this.afterGetList()
            return this.list
        } else {
            this.has_next = false
            this.loading = false
            return []
        }
    }
    afterGetList() { }
    async befaultGetList(options: any) { }
    async deleteItem(index: number): Promise<boolean> {
        const { id } = this.list[index]
        const res = await this.request(this.url.delete, { method: 'POST', parames: { id } })
        if (res) {
            this.list.splice(index, 1)
            this.total--
            return true
        }
        return false
    }
    async deleteItems() {
        if (this.select.length === 0) {
            if (!(window as any).isPhone) {
                ElMessage.warning("当前没有选中项")
            }
            return false
        }
        for (let index = 0; index < this.select.length; index++) {
            const element = this.select[index];
            await this.request(this.url.delete, { method: 'POST', parames: { id: element.id } })
        }
        this.refresh(1)
    }
}